# import
import numpy as np
import pandas as pd

import time
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from util.models import *
from util.misc import *
from completeness import *

## mat file
from scipy.io import loadmat

if __name__ == '__main__':
    base = os.path.dirname(os.path.abspath(__file__))
    file_name = os.path.normpath(os.path.join(base, '../data/new_data.csv'))
    df_full = pd.read_csv(file_name)

    ## drop lotteries over losses
    df = df_full[df_full['z2'] >= 0]

    # preprocessing
    X, Y, subject_idx, cov_idx, cov_list = preprocess(df)

    # main
    start_time = time.perf_counter()

    alpha_grid_size = 0.01
    eta_grid_size = 0.01
    a_ubd = 1
    e_lbd = -1
    e_ubd = 5

    ## alpha, eta
    alpha_grid = np.arange(alpha_grid_size, a_ubd+alpha_grid_size, alpha_grid_size)
    eta_grid = np.arange(e_lbd, e_ubd+eta_grid_size, eta_grid_size)
    param_grid = cartesian_product(alpha_grid, eta_grid)

    print('DA(alpha, eta)')
    completeness, stderr, model_best_params =\
    cross_validation(X, Y, subject_idx, cov_idx, cov_list, param_grid,
                     pred=pred_DA, train_model=train_DA,
                     base_param=np.array([1,0]), n_splits=10, print_option=True)

    # time
    end_time = time.perf_counter()
    elapsed_time = end_time - start_time
    hour = elapsed_time//3600
    elapsed_time = elapsed_time - hour*3600
    minute = elapsed_time//60
    elapsed_time = elapsed_time - minute*60
    second = elapsed_time

    print('')
    print('process time: {}:{}:{}'.format(int(hour), int(minute), int(second)))
